package ym.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ym.mapper.HouseImgMapper;
import ym.pojo.HouseImg;

import java.util.List;

@Transactional
@DubboService
public class HouseImgServiceImpl implements HouseImgService {
    @Autowired
    private HouseImgMapper houseImgMapper;


    @Override
    public void add(HouseImg houseImg) {
        houseImgMapper.insert(houseImg);
    }

    @Override
    public void update(HouseImg houseImg) {
        houseImgMapper.updateById(houseImg);
    }

    @Override
    public void delete(Integer id) {
        houseImgMapper.deleteById(id);
    }

    @Override
    public HouseImg findById(Integer id) {
        return houseImgMapper.selectById(id);
    }

    @Override
    public List<HouseImg> findAll() {
        return houseImgMapper.selectList(null);
    }

    @Override
    public Page<HouseImg> search(String search, int page, int size) {
        QueryWrapper<HouseImg> queryWrapper = new QueryWrapper<>();
        if (search != null) {
            queryWrapper.like("houseImgName", search);
        }
        return houseImgMapper.selectPage(new Page(page, size), queryWrapper);
    }

    @Override
    public List<HouseImg> findAllByHouseId(Integer houseId) {
        QueryWrapper<HouseImg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("houseId", houseId);
        return houseImgMapper.selectList(queryWrapper);
    }

    @Override
    public Page<HouseImg> searchByHouseId(Integer houseId, String search, int page, int size) {
        QueryWrapper<HouseImg> queryWrapper = new QueryWrapper<>();
        if (search != null) {
            queryWrapper.like("houseImgName", search).eq("houseId", houseId);
        } else {
            queryWrapper.eq("houseId", houseId);
        }

        return houseImgMapper.selectPage(new Page(page, size), queryWrapper);
    }
}
